import { queryTaskPage, queryTargetList, queryFloorPage, updateTarget, updateRuleTask, queryPerson, batch, queryReportPage, queryItemDetails, queryTaskById, copy, create, remove, update, queryReportDetails } from './service';
import { routerRedux } from 'dva/router';
import { queryDictList } from '@/services/dict'
import { message } from 'antd';

export default {
  namespace: 'task_month',

  state: {
    list: { data: [] },
    details: [],
    reportDetails: {},
    taskCond: {},
    reportCond: {},
    targetCond: {},
    floorCond: {},
    locationData: {},
    target: []
  },

  effects: {
    *initTask({ payload }, { put, call }) {
      const user = yield call(queryPerson);
      yield put({ type: 'setUsers', payload: user })
    },
    *initTaskCond(_, { put }) {
      yield put({ type: 'setTaskCond', payload: { pageSize: 10 } });
      yield put({ type: 'queryTaskPage' })
    },
    *updateTaskCond({ payload }, { put, select }) {
      const cond = yield select(state => state.task_month.taskCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setTaskCond', payload: params });
      yield put({ type: 'queryTaskPage' })
    },
    *queryTaskPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.task_month.taskCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryTaskPage, params) || {};
      yield put({ type: 'setTaskPage', payload: { list, total: +total } })
    },
    *copy({ payload }, { put, call }) {
      yield call(copy, payload);
      yield put({ type: 'queryTaskPage' })
    },
    *remove({ payload }, { call, put }) {
      yield call(remove, payload);
      yield put({ type: 'queryTaskPage' });
    },
    *create({ payload }, { call, put }) {
      const id = yield call(create, payload);
      if (id) {
        yield put(routerRedux.push(`/data/task_month/${id}/update`, { isNew: true }));
      }
    },
    *cancel({ payload }, { call, put }) {
      if (payload.isNew) {
        yield call(remove, payload.id);
      }

      yield put(routerRedux.push(`/data/task_month`));
    },
    *updateTargetCond({ payload }, { put, select }) {
      const cond = yield select(state => state.task_month.targetCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setTargetCond', payload: params });
      yield put({ type: 'queryTargetList' })
    },
    *queryTargetList({ payload}, { call, put, select }) {
      const cond = yield select(state => state.task_month.targetCond);
      const params = { ...payload, ...cond };
      const response = yield call(queryTargetList, params);
      yield put({ type: 'setTargetPage', payload: response })
    },
    *queryItemDetails({ payload }, { call, put, select }) {
      let { child, plan = [] } = yield call(queryItemDetails, payload.targetId) || {};
      let ans = [];

      Array.isArray(plan) && child && (
        ans = plan.map(v => ({
          id: v,
          ...child[v],
          children: Array.isArray(child[v].question) 
                      ? child[v].question.map(u => ({ targetId: v, targetDisable: child[v].disable, ...u, id: `${v}#${u.id}` }))
                      : []
        }))
      )

      const thisData = yield select(state => state.task_month.target) || {};
      Array.isArray(thisData) && (thisData[payload.ind].item = ans);

      yield put({ type: 'setTargetItem', payload: { target: thisData, plan } })
    },
    *updateRuleTask({ payload }, { call, put }) {
      yield call(updateRuleTask, payload)
      message.success('保存成功');
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryEditTaskById({ payload }, { call, put }) {
      const response = yield call(queryTaskById, payload.id);
      const items = {};

      payload.keys.forEach(item => {
        items[item] = response[item]
      })
      yield put({ type: 'setEditTaskInfo', payload: items })
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.push('/data/task_month'));
    },
    *queryFloorPage({ payload = { pageNum: 1, pageSize: 10} }, { call, put, select }) {
      const cond = yield select(state => state.task_month.floorCond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryFloorPage, params);
      yield put({ type: 'setFloorPage', payload: { list, total: +total } })
    },
    *initFloorCond(_, { put }) {
      yield put({ type: 'setFloorCond', payload: {} });
      yield put({ type: 'queryFloorPage' })
    },
    *updateFloorCond({ payload }, { select, put }) {
      const cond = yield select(state => state.task_month.floorCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setFloorCond', payload: params });
      yield put({ type: 'queryFloorPage' })
    },
    *targetBatch({ payload }, { call, put }) {
      yield call(batch, payload);
      yield put({ type: 'queryTargetList', payload: { ruleId: payload.ruleId } })
    },
    *delTagert({ payload }, { call, put }) {
      const { ruleId, ...params } = payload;
      yield call(updateTarget, params);
      yield put({ type: 'queryTargetList', payload: { ruleId } })
    },
    *queryReportPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.task_month.reportCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryReportPage, params);
      yield put({ type: 'setReportPage', payload: { list, total: +total } })
    },
    *updateReportCond({ payload }, { put, select }) {
      const cond = yield select(state => state.task_month.reportCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setReportCond', payload: params });
      yield put({ type: 'queryReportPage' })
    },
    *queryReportDetails({ payload }, { put, call }) {
      const response = yield call(queryReportDetails, payload);
      yield put({ type: 'setReportDetails', payload: response })
    }
  },

  reducers: {
    setTaskCond(state, action) {
      return {
        ...state,
        taskCond: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setTaskPage(state, action) {
      return {
        ...state,
        taskList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setTargetPage(state, action) {
      return {
        ...state,
        target: action.payload
      }
    },
    setFloorCond(state, action) {
      return {
        ...state,
        floorCond: action.payload
      }
    },
    setFloorPage(state, action) {
      return {
        ...state,
        floorList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditTaskInfo(state, action) {
      return {
        ...state,
        editTaskDetails: action.payload
      }
    },
    setUsers(state, action) {
      return {
        ...state,
        users: action.payload
      }
    },
    setReportCond(state, action) {
      return {
        ...state,
        reportCond: action.payload
      }
    },
    setTargetItem(state, action) {
      return {
        ...state,
        target: action.payload.target,
        plan: action.payload.plan
      }
    },
    setReportPage(state, action) {
      return {
        ...state,
        reportList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setReportDetails(state, action) {
      return {
        ...state,
        reportDetails: action.payload
      }
    }
  },
}
